package com.example.thecharacterbook;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;


public class TheCheck_MYSELF extends Fragment {
  private CHECKMESELF listener;
    private String Word, Mean, Example, Word_Id,Example2,Example3;
    static private TextView Check_Word1, Check_Mean, Check_ID, CHECK_Example,CHECK_Example2,CHECK_Example3;
    private ImageButton uk_button,en_button,EXAMPLE_BTN1,EXAMPLE_BTN2,EXAMPLE_BTN3,STOP;
    private Button DELETE;
    private Context context;
    private FloatingActionButton CheckBtn;
    private MediaPlayer mediaPlayer;
    private BottomNavigationView bottomNavigationView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        }

    public interface CHECKMESELF{
       void ChangeVar(String Word,String ID,String Mean,String E1,String E2,String E3);
    }

    public void SetVar(String Word,String ID,String Mean,String E1,String E2,String E3){
        Log.e("Wooooooooooooooooooooord231313","is "+Word);
       Check_Word1.setText(Word);
        Check_Mean.setText(Mean);
        Check_ID.setText(ID);
        CHECK_Example.setText(E1);
        CHECK_Example2.setText(E2);
        CHECK_Example3.setText(E3);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.scrollcheck, container, false);
        uk_button = v.findViewById(R.id.SoundBtnA);
        en_button = v.findViewById(R.id.SoundBtn2A);
        Check_Word1 = v.findViewById(R.id.CHECK_WORDA);
        Check_Mean = v.findViewById(R.id.CHECK_MEANA);
        Check_ID = v.findViewById(R.id.CHECK_NUMA);
        CheckBtn=v.findViewById(R.id.UPDATE_FLOATA);
        CHECK_Example = v.findViewById(R.id.CHECK_EXAMPLE1A);
        CHECK_Example2 = v.findViewById(R.id.CHECK_EXAMPLE2A);
        CHECK_Example3 = v.findViewById(R.id.CHECK_EXAMPLE3A);
        EXAMPLE_BTN1=v.findViewById(R.id.SoundBtnEXAPMBLE1A);
        EXAMPLE_BTN2=v.findViewById(R.id.SoundBtnEXAPMBLE2A);
        EXAMPLE_BTN3=v.findViewById(R.id.SoundBtnEXAPMBLE3A);
        STOP=v.findViewById(R.id.STOPA);
        STOP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();
            }
        });
        CheckBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent  = new Intent(getContext(),UpDate_PAGE.class);
                intent.putExtra("Word",Check_Word1.getText().toString());
                intent.putExtra("Word_Id",Check_ID.getText().toString());
                intent.putExtra("Mean",Check_Mean.getText().toString());
                intent.putExtra("Example",CHECK_Example.getText().toString());
                intent.putExtra("Example2",CHECK_Example2.getText().toString());
                intent.putExtra("Example3",CHECK_Example3.getText().toString());
                startActivity(intent);
            }
        });
        String uk = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Word);
        String en = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "en", Word);
        String e1 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example);
        String e2 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example2);
        String e3 = String.format("https://fanyi.baidu.com/gettts?lan=%s&text=%s&spd=3&source=web", "uk", Example3);
        EXAMPLE_BTN1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e1));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        EXAMPLE_BTN2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e2));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        EXAMPLE_BTN3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {

                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(e3));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        en_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {

                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(en));
                    mediaPlayer.start();
                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        uk_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    mediaPlayer =MediaPlayer.create(view.getContext(), Uri.parse(uk));
                    mediaPlayer.start();

                } catch (Exception e) {
                    Log.e("Error,trigger!", "发音错误！");
                }
            }
        });
        return v;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if(context instanceof CHECKMESELF){
            listener = (CHECKMESELF) context;
        }
        else
        {
            throw  new RuntimeException(context.toString()+"需要实现你的接口");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener=null;
    }
}

